package com.worms.http.util;

import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * html工具类
 *
 * @auth staywood_zjb
 * @date 2018/04/3 21:06
 *
 * <p>主要作用</p>
 * 1.解析响应结果的url中的链接
 * 2.判断链接是否符合抓取要的规则
 * 3.判断url是否需要补全
 *
 */
public class HtmlResponseUtil {

    /**
     * 解析html其中符合规则的链接
     * @param rspstr
     * @param patternList
     * @return
     */
    public static List<String> handleUrl(String rspstr, List<String> patternList){
        if(patternList == null
                || patternList.size() == 0
                || StringUtils.isEmpty(rspstr)){
            return Collections.EMPTY_LIST;
        }
        Set<String> grabSet = new HashSet<String>();
        for(int i = 0;i < patternList.size() ;i++){
            Pattern p = Pattern.compile(patternList.get(i));
            Matcher m = p.matcher(rspstr);
            while(m.find()) {
                grabSet.add(m.group());
            }
        }
        List<String> grabList = new ArrayList<String>(grabSet);
        return grabList;
    }

    /**
     * 判断该链接是否符合规则
     * @param url
     * @param patternList
     * @return
     */
    public static Boolean panIsPatternUrl(String url, List<String> patternList){
        if(patternList == null
                || patternList.size() == 0
                || StringUtils.isEmpty(url)){
            return false;
        }
        Boolean pan = false;
        Set<String> grabSet = new HashSet<String>();
        for(int i = 0;i < patternList.size() ;i++){
            Pattern p = Pattern.compile(patternList.get(i));
            Matcher m = p.matcher(url);
            if(m.matches()) {
                pan = true;
                break;
            }
        }
        return pan;
    }

    /**
     * 新的链接
     * 1.url补全分析
     * @param newGrabUrlList
     */
    public static void newGrabUrlHandle(String grabUrl, List<String> newGrabUrlList){
        if(StringUtils.isEmpty(grabUrl) || newGrabUrlList == null || newGrabUrlList.size() == 0){
            return;
        }
        String host = "http://";
        if(grabUrl.contains("http://")){
            grabUrl = grabUrl.substring(grabUrl.indexOf("http://") + 7, grabUrl.length());
        }
        if(grabUrl.contains("https://")){
            grabUrl = grabUrl.substring(grabUrl.indexOf("https://") + 8, grabUrl.length());
            host = "https://";
        }
        String hostName = grabUrl.substring(0, grabUrl.indexOf("/"));
        for (int i = 0; i < newGrabUrlList.size(); i++) {
            String newGrabUrl = newGrabUrlList.get(i);
            if(!isTopURL(newGrabUrl)){
                newGrabUrlList.set(i, host + hostName + newGrabUrl);
            }
        }
    }

    /**
     * 判断是否是网址
     * @param str
     * @return
     */
    public static boolean isTopURL(String str){
        // 转换为小写
        str = str.toLowerCase();
        String domainRules = "com.cn|net.cn|org.cn|gov.cn|com.hk|公司|中国|网络|com|net|org|int|edu|gov|mil|arpa|Asia|biz|info|name|pro|coop|aero|museum|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cf|cg|ch|ci|ck|cl|cm|cn|co|cq|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|es|et|ev|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gp|gr|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|ml|mm|mn|mo|mp|mq|mr|ms|mt|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|va|vc|ve|vg|vn|vu|wf|ws|ye|yu|za|zm|zr|zw";
        String regex = "^((https|http|ftp|rtsp|mms)?://)"
                + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
                + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
                + "|" // 允许IP和DOMAIN（域名）
                + "(([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]+\\.)?" // 域名- www.
                + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 二级域名
                + "("+domainRules+"))" // first level domain- .com or .museum
                + "(:[0-9]{1,4})?" // 端口- :80
                + "((/?)|" // a slash isn't required if there is no file name
                + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher isUrl = pattern.matcher(str);
        return isUrl.matches();
    }

}
